package com.zhida.smartcontrol.pagehome;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.jay.tools.utils.SharedUtil;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.Keys;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;
import com.zhida.smartcontrol.pagectrl.ProjectListBean;
import com.zhida.smartcontrol.pagectrl.projectdetail.warning.WarningBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: tanjianpei
 * date: 2019/10/8 008 15:33
 * description:
 */
public class PageHomePresenter extends BasePresenterImpl<PageHomeContract.View> implements PageHomeContract.Presenter {

    @Override
    public void requestProjectList() {
        String json = SharedUtil.getString(Keys.PROJECT_LIST, "");
        if (TextUtils.isEmpty(json)) {
            Params params = Params.newBuilder()
                    .add("projectName", "")
                    .build();
            mKalleCenter.get(Api.PROJECT_LIST, params, true, new KalleCallBack() {
                @Override
                public void onSuccess(String result, String msg) {
                    SharedUtil.putString(Keys.PROJECT_LIST, result);
                    parseProjectList(result);
                }

                @Override
                public void onFailed(String msg) {
                    mView.showMsg(msg);
                }
            });
        } else {
            parseProjectList(json);
        }
    }

    private void parseProjectList(String result) {
        List<ProjectListBean> projectList = JSON.parseArray(result, ProjectListBean.class);
        mView.showProjectId(projectList.get(0).getId());
    }

    // 根据项目 ID 和查询条件获取该项目下的所有设备
    @Override
    public void requestByProjectAndKeyWord(String projectBaseId) {
        Params params = Params.newBuilder()
                .add("projectBaseId", projectBaseId)
                .add("keywords", "")
                .build();
        mKalleCenter.get(Api.FIND_PROJECT, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                List<ProjectResultBean> resultBeans = JSON.parseArray(result, ProjectResultBean.class);
                mView.showList(resultBeans);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
    @Override
    public void requestWarning(String projectBaseId) {
        if (TextUtils.isEmpty(projectBaseId)) {
            return;
        }
        Params params = Params.newBuilder()
                .add("pageNo", 1)
                .add("projectBaseId", projectBaseId)
                .add("pageSize", 10)
                .add("type", "")
                .build();
        mKalleCenter.get(Api.WARNING_LIST, params, false, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                WarningBean warningBean = JSON.parseObject(result, WarningBean.class);
                mView.showWarningData(warningBean);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
    @Override
    public void requestLampData(String id) {
        Params params = Params.newBuilder()
                .add("id", id)
                .build();
        mKalleCenter.get(Api.GET_LAMP_SWITCH_BY_ID, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                LampBean lampBean = JSON.parseObject(result, LampBean.class);
                mView.showPop(lampBean);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void changeMode(List<String> codeList, int type,String switchType) {
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", codeList);
        params.put("type", type);//1手动，0自动
        mKalleCenter.postJson(Api.HAND_AUTO_SET, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.changeMode(type,switchType);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void openClose(List<String> codeList, String type, String switchType) {
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", codeList);
        params.put("type", type);// 1：开启（灯） 0：关闭（灯）
        mKalleCenter.postJson(Api.OPEN_CLOSE_SWITCH, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.openCloseOk(type.equals("1"),switchType);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void openCloseNbDouble(List<String> codeList,String type, String value, String switchType) {
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", codeList);
        params.put("type", type);//type  1：主灯2：辅灯3：主辅灯全选
        params.put("value", value);// 1：开启（灯） 0：关闭（灯）
        mKalleCenter.postJson(Api.OPEN_CLOSE_NB_DOUBLE, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
//                mView.openCloseOk(value.equals("1"),bean.getSwitchType());
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void requestTime(String switchType, List<LampBean> codeList) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                if (switchType.equals(Const.ZIG_BEE_SINGLE)) {
                    list.add(lampBean.getId());
                } else {
                    list.add(lampBean.getCode());
                }
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", switchType);
        mKalleCenter.postJson(Api.CORRECTION_TIME, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
//                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
}
